<?php
/**
 * 投融事件
 */
namespace app\model;

use think\Model;
use app\model\Chain;
use app\model\ChainModel;
use app\model\CityModel;

class InvestEventModel extends Model
{
	protected $name = 'invest_event';
	
	// 设置当前模型的数据库连接
	protected $connection ='db_yunduan';
	/**
	 * 投融事件列表
	 *
	 */
	public function GetList($field='*',$condition=array(),$page=1,$limit=15){
		
		$obj = new InvestEventModel();
		$obj=$obj->alias('a');
		$where="a.status=1 ";
		/***关键字*/
		
		if(isset($condition['keywords'])&&!empty($condition['keywords'])){
			$where.=" and a.event_name like '%{$condition['keywords']}%' ";
		}
		/***行业*/
		if(isset($condition['industry_id'])&&!empty($condition['industry_id'])){
			$obj=$obj->join('invest_event_industry b','a.id=b.invest_id','left');
			$where.=" and b.status=1 and b.industry_id={$condition['industry_id']} ";
		}
		
		/***云端产业链*/
		if(isset($condition['yun_chain_id'])&&!empty($condition['yun_chain_id'])){
			$obj=$obj->join('invest_event_chain c','a.id=c.invest_id','left');
			$where.=" and c.status=1 and c.chain_id={$condition['yun_chain_id']} ";
		}
		/***地方产业链*/
		if(isset($condition['chain_id'])&&!empty($condition['chain_id'])){
			$ChainModel=new Chain();
			$chain_id=$ChainModel->GetChainIdById($condition['chain_id']);
			
			$obj=$obj->join('invest_event_chain c','a.id=c.invest_id','left');
			$where.=" and c.status=1 and c.chain_id=$chain_id ";
		}else{
			//全部产业链
			if(isset($condition['user_id'])&&!empty($condition['user_id'])){
				$ChainModel=new Chain();
				$chain_ids=$ChainModel->GetChainListByUserId($condition['user_id']);
				if(!empty($chain_ids)){
					$chain_ids=implode(',',$chain_ids);
					$where.=" and c.chain_id in ($chain_ids) ";
				}
			}
		}
		
		/***城市*/
		if(isset($condition['city_id'])&&!empty($condition['city_id'])){
			$where.=" and (a.country_id={$condition['city_id']} or a.province_id={$condition['city_id']} or a.city_id={$condition['city_id']} or a.area_id={$condition['city_id']} ) ";
		}
		
		/***类型*/
		if(isset($condition['type_id'])&&!empty($condition['type_id'])){
			$where.=" and FIND_IN_SET('{$condition['type_id']}',a.type_id) ";
		}
		$listobj=$obj->field($field)->where($where)->order('a.id','desc')->paginate($limit)->toArray();
		
		
		foreach ($listobj['data'] as &$val){
			$val['city_name']=$this->GetCityName($val);
			if(isset($val['turns'])){
				$val['turns']=$this->GetTurns($val['turns']);
			}
		}
		return $listobj;
	}
	/**
	 * 投融事件详情
	 *
	 */
	public function detail($id){
		$InvestEventModel = InvestEventModel::find($id);
		return $InvestEventModel;
	}
	/**
	 * 获取园区数量
	 */
	public function GetTotal($condition=array()){
		$obj = new InvestEventModel();
		$obj=$obj->alias('a');
		$where="a.status=1 ";
		/***关键字*/
		
		if(isset($condition['keywords'])&&!empty($condition['keywords'])){
			$where.=" and a.event_name like '%{$condition['keywords']}%' ";
		}
		/***行业*/
		if(isset($condition['industry_id'])&&!empty($condition['industry_id'])){
			$obj=$obj->join('invest_event_industry b','a.id=b.invest_id','left');
			$where.=" and b.status=1 and b.industry_id={$condition['industry_id']} ";
		}
		
		/***云端产业链*/
		if(isset($condition['yun_chain_id'])&&!empty($condition['yun_chain_id'])){
			$obj=$obj->join('invest_event_chain c','a.id=c.invest_id','left');
			$where.=" and c.status=1 and c.chain_id={$condition['yun_chain_id']} ";
		}
		/***地方产业链*/
		if(isset($condition['chain_id'])&&!empty($condition['chain_id'])){
			$ChainModel=new Chain();
			$chain_id=$ChainModel->GetChainIdById($condition['chain_id']);
			
			$obj=$obj->join('invest_event_chain c','a.id=c.invest_id','left');
			$where.=" and c.status=1 and c.chain_id=$chain_id ";
		}else{
			//全部产业链
			if(isset($condition['user_id'])&&!empty($condition['user_id'])){
				$ChainModel=new Chain();
				$chain_ids=$ChainModel->GetChainListByUserId($condition['user_id']);
				if(!empty($chain_ids)){
					$chain_ids=implode(',',$chain_ids);
					$where.=" and c.chain_id in ($chain_ids) ";
				}
			}
		}
		
		/***城市*/
		if(isset($condition['city_id'])&&!empty($condition['city_id'])){
			$where.=" and (a.country_id={$condition['city_id']} or a.province_id={$condition['city_id']} or a.city_id={$condition['city_id']} or a.area_id={$condition['city_id']} ) ";
		}
		
		$total=$obj->where($where)->count();
		
		return $total;
	}
	/**
	 * 获得城市
	 */
	public function GetCityName($info){
		$CityModel=new CityModel();
		$param=[];
		if(isset($info['province_id'])){
			$param[]=$info['province_id'];
		}
		if(isset($info['city_id'])){
			$param[]=$info['city_id'];
		}
		if(isset($info['area_id'])){
			$param[]=$info['area_id'];
		}
		
		$city=$CityModel->GetCityNameByCityIds($param);
		return empty($city)?'':implode(',',$city);
		
	}

	/**
	 * 获得产业
	 */
	public function GetChainName($info){
		$ChainModel=new ChainModel();
		
		$res=$ChainModel->GetChainNameByInvestEvent($info['id']);
		return implode(',',$res);
	}
	/**
	 * 获得轮次
	 */
	private function GetTurns($id){
		$data=[
				'0'=>'天使轮',
				'1'=>'A轮',
				'2'=>'B轮',
				'3'=>'C轮',
				'4'=>'D轮',
				'5'=>'E轮',
		];
		if(isset($data[$id])){
			return $data[$id];
		}else{
			return '未知';
		}
	}
}